package com.wallet.admin.controller.system;



import com.wallet.admin.feign.system.MenuFeignClient;

import com.wallet.common.util.PageUtil;
import com.wallet.common.util.StringUtil;
import com.wallet.common.vo.ApiResult;
import com.wallet.common.vo.DataResult;
import com.wallet.common.vo.PageResult;
import com.wallet.provider.model.Menu;
import com.wallet.provider.model.Role;
import com.wallet.provider.model.vo.TreeNodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/system/menu")
public class MenuController {

    @Autowired
    private MenuFeignClient menuFeignClient;

    /**
     * 分页查询系统菜单
     * @param offset
     * @param limit
     * @param order
     * @param ordername
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public DataResult list(@RequestParam(defaultValue = "1") int offset, @RequestParam(defaultValue = "10")  int limit, @RequestParam(defaultValue = "createTime")  String order, @RequestParam(defaultValue = "desc")  String ordername, String search){
        PageResult<Menu> menus = menuFeignClient.getMenuList(PageUtil.getPageNum(offset, limit), limit, StringUtil.camel2Underline(order), ordername);
        return new DataResult.Builder<>().build(menus);
    }

    /**
     * 获取菜单树
     * @return
     */
    @RequestMapping("/getMenuTree")
    @ResponseBody
    public List<TreeNodeVo> getMenuTree(){
        List<TreeNodeVo> menus = menuFeignClient.getMenuTree();
        return menus;
    }

    /**
     * 跳到修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Long id, Model model){
        Menu menu = menuFeignClient.getMenuById(id);
        model.addAttribute("menu", menu);
        return "system/menu_edit";
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ApiResult<Role> update(Menu menu){
        Boolean rs = menuFeignClient.updateMenu(menu);
        return new ApiResult.Builder<>().success(rs).build();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ApiResult<Role> delete(Long[] ids){
        Boolean rs = menuFeignClient.deleteMenu(ids);
        return new ApiResult.Builder<>().success(rs).build();
    }

}

